<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>物流管理系统</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Segoe UI', 'Microsoft YaHei', Arial, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
        }

        .container {
            background: white;
            border-radius: 20px;
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
            padding: 40px;
            width: 100%;
            max-width: 500px;
            text-align: center;
        }

        .header {
            margin-bottom: 30px;
        }

        .header h1 {
            color: #333;
            font-size: 28px;
            margin-bottom: 10px;
        }

        .header p {
            color: #666;
            font-size: 14px;
        }

        .user-type-container {
            display: flex;
            background: #f8f9fa;
            border-radius: 10px;
            padding: 5px;
            margin-bottom: 30px;
        }

        .user-type-tab {
            flex: 1;
            padding: 12px 20px;
            border: none;
            background: transparent;
            border-radius: 8px;
            cursor: pointer;
            font-size: 14px;
            font-weight: 500;
            color: #666;
            transition: all 0.3s ease;
        }

        .user-type-tab.active {
            background: white;
            color: #667eea;
            box-shadow: 0 2px 8px rgba(102, 126, 234, 0.2);
        }

        .tab-container {
            display: flex;
            background: #f8f9fa;
            border-radius: 10px;
            padding: 5px;
            margin-bottom: 30px;
        }

        .tab {
            flex: 1;
            padding: 12px 20px;
            border: none;
            background: transparent;
            border-radius: 8px;
            cursor: pointer;
            font-size: 14px;
            font-weight: 500;
            color: #666;
            transition: all 0.3s ease;
        }

        .tab.active {
            background: white;
            color: #667eea;
            box-shadow: 0 2px 8px rgba(102, 126, 234, 0.2);
        }

        .form-container {
            display: none;
        }

        .form-container.active {
            display: block;
        }

        .form-group {
            margin-bottom: 20px;
            text-align: left;
        }

        .form-group label {
            display: block;
            margin-bottom: 8px;
            color: #333;
            font-weight: 500;
        }

        .form-group input {
            width: 100%;
            padding: 12px 15px;
            border: 2px solid #e9ecef;
            border-radius: 10px;
            font-size: 14px;
            transition: all 0.3s ease;
        }

        .form-group input:focus {
            outline: none;
            border-color: #667eea;
            box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
        }

        .submit-btn {
            width: 100%;
            padding: 15px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border: none;
            border-radius: 10px;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
            margin-top: 10px;
        }

        .submit-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 10px 20px rgba(102, 126, 234, 0.3);
        }

        .submit-btn:active {
            transform: translateY(0);
        }

        .message {
            margin-top: 20px;
            padding: 15px;
            border-radius: 10px;
            font-size: 14px;
            display: none;
        }

        .message.success {
            background: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }

        .message.error {
            background: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }

        .message.show {
            display: block;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>物流管理系统</h1>
            <p>基于区块链的物流管理平台</p>
        </div>

        <!-- 用户类型选择 -->
        <div class="user-type-container">
            <button class="user-type-tab active" data-type="user">普通用户</button>
            <button class="user-type-tab" data-type="shipper">货主</button>
            <button class="user-type-tab" data-type="carrier">承运商</button>
        </div>

        <!-- 操作类型选择 -->
        <div class="tab-container">
            <button class="tab active" data-tab="register">注册</button>
            <button class="tab" data-tab="login">登录</button>
        </div>

        <!-- 普通用户注册表单 -->
        <div class="form-container active" id="user-register">
            <form>
                <div class="form-group">
                    <label for="user-reg-username">用户名</label>
                    <input type="text" id="user-reg-username" name="username" required>
                </div>
                <div class="form-group">
                    <label for="user-reg-password">密码</label>
                    <input type="password" id="user-reg-password" name="password" required>
                </div>
                <button type="submit" class="submit-btn">注册用户</button>
            </form>
        </div>

        <!-- 普通用户登录表单 -->
        <div class="form-container" id="user-login">
            <form>
                <div class="form-group">
                    <label for="user-login-id">用户ID</label>
                    <input type="number" id="user-login-id" name="id" required>
                </div>
                <div class="form-group">
                    <label for="user-login-password">密码</label>
                    <input type="password" id="user-login-password" name="password" required>
                </div>
                <button type="submit" class="submit-btn">登录</button>
            </form>
        </div>

        <!-- 货主注册表单 -->
        <div class="form-container" id="shipper-register">
            <form>
                <div class="form-group">
                    <label for="shipper-reg-name">货主姓名</label>
                    <input type="text" id="shipper-reg-name" name="name" required>
                </div>
                <div class="form-group">
                    <label for="shipper-reg-company">公司名称</label>
                    <input type="text" id="shipper-reg-company" name="company" required>
                </div>
                <div class="form-group">
                    <label for="shipper-reg-phone">联系电话</label>
                    <input type="tel" id="shipper-reg-phone" name="phone" required>
                </div>
                <div class="form-group">
                    <label for="shipper-reg-password">密码</label>
                    <input type="password" id="shipper-reg-password" name="password" required>
                </div>
                <button type="submit" class="submit-btn">注册货主</button>
            </form>
        </div>

        <!-- 货主登录表单 -->
        <div class="form-container" id="shipper-login">
            <form>
                <div class="form-group">
                    <label for="shipper-login-id">货主ID</label>
                    <input type="number" id="shipper-login-id" name="id" required>
                </div>
                <div class="form-group">
                    <label for="shipper-login-password">密码</label>
                    <input type="password" id="shipper-login-password" name="password" required>
                </div>
                <button type="submit" class="submit-btn">登录</button>
            </form>
        </div>

        <!-- 承运商注册表单 -->
        <div class="form-container" id="carrier-register">
            <form>
                <div class="form-group">
                    <label for="carrier-reg-name">承运商姓名</label>
                    <input type="text" id="carrier-reg-name" name="name" required>
                </div>
                <div class="form-group">
                    <label for="carrier-reg-info">公司信息</label>
                    <input type="text" id="carrier-reg-info" name="info" required>
                </div>
                <div class="form-group">
                    <label for="carrier-reg-password">密码</label>
                    <input type="password" id="carrier-reg-password" name="password" required>
                </div>
                <button type="submit" class="submit-btn">注册承运商</button>
            </form>
        </div>

        <!-- 承运商登录表单 -->
        <div class="form-container" id="carrier-login">
            <form>
                <div class="form-group">
                    <label for="carrier-login-id">承运商ID</label>
                    <input type="number" id="carrier-login-id" name="id" required>
                </div>
                <div class="form-group">
                    <label for="carrier-login-password">密码</label>
                    <input type="password" id="carrier-login-password" name="password" required>
                </div>
                <button type="submit" class="submit-btn">登录</button>
            </form>
        </div>

        <div class="message" id="message"></div>
    </div>

    <script>
        // 服务端地址配置
        const SERVER_URL = 'http://127.0.0.1:8080';
        
        let currentUserType = 'user';
        let currentTab = 'register';

        // 用户类型切换
        document.querySelectorAll('.user-type-tab').forEach(tab => {
            tab.addEventListener('click', function() {
                document.querySelectorAll('.user-type-tab').forEach(t => t.classList.remove('active'));
                this.classList.add('active');
                currentUserType = this.dataset.type;
                updateFormDisplay();
            });
        });

        // 操作类型切换
        document.querySelectorAll('.tab').forEach(tab => {
            tab.addEventListener('click', function() {
                document.querySelectorAll('.tab').forEach(t => t.classList.remove('active'));
                this.classList.add('active');
                currentTab = this.dataset.tab;
                updateFormDisplay();
            });
        });

        function updateFormDisplay() {
            // 隐藏所有表单
            document.querySelectorAll('.form-container').forEach(form => {
                form.classList.remove('active');
            });

            // 显示当前表单
            const currentForm = document.getElementById(`${currentUserType}-${currentTab}`);
            if (currentForm) {
                currentForm.classList.add('active');
            }
        }

        function showMessage(text, type) {
            const message = document.getElementById('message');
            message.textContent = text;
            message.className = `message ${type} show`;
            setTimeout(() => {
                message.classList.remove('show');
            }, 3000);
        }

        // 普通用户注册
        document.getElementById('user-register').querySelector('form').addEventListener('submit', async function(e) {
            e.preventDefault();
            const formData = new FormData(this);
            const data = Object.fromEntries(formData);
            
            try {
                const response = await fetch(`${SERVER_URL}/user/add`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded',
                    },
                    body: new URLSearchParams({
                        username: data.username,
                        password: data.password
                    })
                });

                const result = await response.json();
                if (result.code === '0') {
                    showMessage('用户注册成功！', 'success');
                    this.reset();
                } else {
                    showMessage(`注册失败：${result.message}`, 'error');
                }
            } catch (error) {
                showMessage('注册失败：网络错误', 'error');
            }
        });

        // 普通用户登录
        document.getElementById('user-login').querySelector('form').addEventListener('submit', async function(e) {
            e.preventDefault();
            const formData = new FormData(this);
            const data = Object.fromEntries(formData);
            
            try {
                const response = await fetch(`${SERVER_URL}/user/check`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded',
                    },
                    body: new URLSearchParams({
                        id: data.id,
                        password: data.password
                    })
                });

                const result = await response.json();
                if (result.code === '0') {
                    showMessage('用户登录成功！', 'success');
                    this.reset();
                } else {
                    showMessage(`登录失败：${result.message}`, 'error');
                }
            } catch (error) {
                showMessage('登录失败：网络错误', 'error');
            }
        });

        // 货主注册
        document.getElementById('shipper-register').querySelector('form').addEventListener('submit', async function(e) {
            e.preventDefault();
            const formData = new FormData(this);
            const data = Object.fromEntries(formData);
            
            try {
                const response = await fetch(`${SERVER_URL}/shipper/register`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded',
                    },
                    body: new URLSearchParams({
                        name: data.name,
                        company: data.company,
                        phone: data.phone,
                        password: data.password
                    })
                });

                const result = await response.json();
                if (result.code === '0') {
                    showMessage('货主注册成功！', 'success');
                    this.reset();
                } else {
                    showMessage(`注册失败：${result.message}`, 'error');
                }
            } catch (error) {
                showMessage('注册失败：网络错误', 'error');
            }
        });

        // 货主登录
        document.getElementById('shipper-login').querySelector('form').addEventListener('submit', async function(e) {
            e.preventDefault();
            const formData = new FormData(this);
            const data = Object.fromEntries(formData);
            
            try {
                const response = await fetch(`${SERVER_URL}/shipper/login`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded',
                    },
                    body: new URLSearchParams({
                        id: data.id,
                        password: data.password
                    })
                });

                const result = await response.json();
                if (result.code === '0') {
                    showMessage('货主登录成功！', 'success');
                    this.reset();
                } else {
                    showMessage(`登录失败：${result.message}`, 'error');
                }
            } catch (error) {
                showMessage('登录失败：网络错误', 'error');
            }
        });

        // 承运商注册
        document.getElementById('carrier-register').querySelector('form').addEventListener('submit', async function(e) {
            e.preventDefault();
            const formData = new FormData(this);
            const data = Object.fromEntries(formData);
            
            try {
                const response = await fetch(`${SERVER_URL}/carrier/register`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded',
                    },
                    body: new URLSearchParams({
                        name: data.name,
                        info: data.info,
                        password: data.password
                    })
                });

                const result = await response.json();
                if (result.code === '0') {
                    showMessage('承运商注册成功！', 'success');
                    this.reset();
                } else {
                    showMessage(`注册失败：${result.message}`, 'error');
                }
            } catch (error) {
                showMessage('注册失败：网络错误', 'error');
            }
        });

        // 承运商登录
        document.getElementById('carrier-login').querySelector('form').addEventListener('submit', async function(e) {
            e.preventDefault();
            const formData = new FormData(this);
            const data = Object.fromEntries(formData);
            
            try {
                const response = await fetch(`${SERVER_URL}/carrier/login`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded',
                    },
                    body: new URLSearchParams({
                        id: data.id,
                        password: data.password
                    })
                });

                const result = await response.json();
                if (result.code === '0') {
                    showMessage('承运商登录成功！', 'success');
                    this.reset();
                } else {
                    showMessage(`登录失败：${result.message}`, 'error');
                }
            } catch (error) {
                showMessage('登录失败：网络错误', 'error');
            }
        });

        // 初始化显示
        updateFormDisplay();
    </script>
</body>
</html>